package com.hnevc.getpostnetdemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2017/10/31.
 */

public class NetAccess {
    public static final String BaseURL = "http://10.1.46.115:8080/RestaurantServer";

    public static String getFoods() {
        try {
            String loginUrl = "/Food/getFoods";
            URL url = new URL(BaseURL + loginUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //准备数据，设置相应的属性
            int code = conn.getResponseCode();
            if (code == 200) {

                BufferedReader reader
                        = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer();
                while ((lines = reader.readLine()) != null) {
                    lines = new String(lines.getBytes(), "utf-8");
                    sb.append(lines);
                }
                String returnData = sb.toString();
                return returnData;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }
    public static InputStream getImageFile(String picName){

        try {
            String loginUrl = "/uploadFoodImage/";
            URL url = new URL(BaseURL + loginUrl+picName);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            int code = conn.getResponseCode();
            if(code == 200){
                InputStream in = conn.getInputStream();
                return  in;
            }else {
                return null;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
